//JavaScript代码区域
layui.use('element', function(){
  var element = layui.element;
  
});

layui.use(['form','upload', 'layedit', 'laydate'], function(){
  var form = layui.form
  ,layer = layui.layer
  ,layedit = layui.layedit
  ,laydate = layui.laydate
  var $ = layui.jquery
  ,upload = layui.upload;
  
  //日期
  laydate.render({
    elem: '#date'
  });
  laydate.render({
    elem: '#date1'
  });
  
  //创建一个编辑器
  var editIndex = layedit.build('LAY_demo_editor');
 
  //自定义验证规则
  form.verify({
    title: function(value){
      if(value.length < 5){
        return '标题至少得5个字符啊';
      }
    }
    ,pass: [/(.+){6,12}$/, '密码必须6到12位']
    ,content: function(value){
      layedit.sync(editIndex);
    }
  });
  

  
  //监听提交
  form.on('submit(demo1)', function(data){
    layer.alert(JSON.stringify(data.field), {
      title: '最终的提交信息'
    })
    return true;
  });
 
  //表单初始赋值
  form.val('example', {
    "username": "贤心" // "name": "value"
    ,"password": "123456"
    ,"interest": 1
    ,"like[write]": true //复选框选中状态
    ,"close": true //开关状态
    ,"sex": "女"
    ,"desc": "我爱 layui"
  });

//多图片上传
upload.render({
		    elem: '.myupload'
		    ,url:'/travel/strategy/upload.do'
		    ,multiple: true
		    ,before: function(obj){
		        //预读本地文件示例，不支持ie8
		        obj.preview(function(index, file, result){
		          $('#demo2').append('<img src="'+ result +'" alt="'+ file.name +'" class="layui-upload-img" style="width:100px;height:60px;">')
		        });
		      }
		    ,done: function(res){
		      //上传完毕
		    	alert(res.msg +""+ res.picid1);
		    }
		  });
  });  






$("#addpage").click(function(){
	var $pages=$('<div class="pageone"></div>')
	var $hr=$('<hr class="layui-bg-red">')
	var $p=$('<div class="layui-form-item">'
					+'<label class="layui-form-label">请输入段落标题：</label>'
					+'<div class="layui-input-block">'
						+'<input type="text" name="title" lay-verify="title"'
							+'autocomplete="off" placeholder="请输入段落标题" class="layui-input">'
					+'</div>'
				+'</div>')
	var $page=$('<div class="layui-form-item layui-form-text">'
					+'<label class="layui-form-label">请输入段落内容:</label>'
					+'<div class="layui-input-block">'
						+'<textarea placeholder="请输入段落内容" class="layui-textarea"></textarea>'
					+'</div>'
				+'</div>')
	var $pic=$('<div class="layui-upload">'
			+'<button type="button" class="layui-btn myupload"'
			+'	style="display: block; margin: 0 auto;" id="test2">上传段落信息配图</button>'
			+'<blockquote class="layui-elem-quote layui-quote-nm"'
			+'	style="margin-top: 10px;">'
			+'	预览图：'
			+'	<div class="layui-upload-list" id="demo2"></div>'
			+'</blockquote>'
			+'</div>')
	//var $butten=$('<button id="addpage" class="layui-btn layui-btn-danger">增加下一段内容</button>')
	
	//var $p=$('<p></p>');
	//var $text=$('<input type="text" class="input-text" name="options" />');
	var $del=$('<button id="addpage" class="layui-btn layui-btn-danger" style="display:block;margin:5px auto;">删除这一段内容</button>');
	$del.click(function(){
		$(this).parent().remove();
	});
	$pages.append($hr).append($p).append($page).append($pic).append($del);
	//$p.append($text).append($del);
	$("#pages").append($pages);
});